package com.example.testapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;

import com.example.testapplication.adapter.PersonAdapter;
import com.example.testapplication.bean.Person;

import java.util.ArrayList;
import java.util.List;

public class ListViewCheck extends AppCompatActivity {
    private List<Person> mPersonList = new ArrayList<>(); //定义实物类
    private ListView mListView; // 列表视图
    private PersonAdapter mAdapter; // 适配器

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_view_check);
        initPerson(); // 初始化实体类
        mAdapter = new PersonAdapter(ListViewCheck.this,mPersonList);
        // 构建适配器对象 完成ListView与数据之间关联
        mListView = (ListView) findViewById(R.id.list_check);
        mListView.setAdapter(mAdapter); // 设置适配器
        Button button1 = (Button) findViewById(R.id.selected_all);
        Button button2 = (Button) findViewById(R.id.unselected_all);
        Button button3 = (Button) findViewById(R.id.reverse_select);
        // 为按钮设置监听
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                selectAll(view);
            }
        });

        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                unSelectAll(view);
            }
        });

        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                reverseSelect(view);
            }
        });

    }
    // 反选
    public void reverseSelect(View view){
        for(Person person:mPersonList){
            if(person.isCheck() == false){
                person.setCheck(true);
            }else{
                person.setCheck(false);
            }
            // 刷新listView 和TextView 的显示
            mAdapter.notifyDataSetChanged();

        }
    }
    // 全选
    public void selectAll(View view){
        // 遍历list长度 将MyAdapter 中map值全部设为true
        for(Person person:mPersonList){
            person.setCheck(true);
            // 刷新listView 和TextView 的显示
            mAdapter.notifyDataSetChanged();

        }

    }
    // 全不选
    public void unSelectAll(View view){
        // 遍历list长度 将MyAdapter 中map值全部设为true
        for(Person person:mPersonList){
            person.setCheck(false);
            // 刷新listView 和TextView 的显示
            mAdapter.notifyDataSetChanged();

        }
    }
    // 初始化实体类
    private void initPerson() {
        // 模拟数据
        for(int i =0;i<5;i++){
            Person tom = new Person("金星",R.drawable.jinxing);
            mPersonList.add(tom);
            Person kate = new Person("木星",R.drawable.muxing);
            mPersonList.add(kate);
            Person ross = new Person("水星",R.drawable.shuixing);
            mPersonList.add(ross);

        }
    }
}